﻿namespace DesignModeConsole.行为型.状态模式.MarioSample;

/// <summary>
/// 查表法
/// </summary>
public class MarioStateMachine2
{
    private static readonly StateEnum[][] _transitionTable =
    {
        // 第一层：small  super cape fire
        new[] { StateEnum.Super, StateEnum.Cape, StateEnum.Fire, StateEnum.Small },
        new[] { StateEnum.Super, StateEnum.Cape, StateEnum.Fire, StateEnum.Small },
        new[] { StateEnum.Cape, StateEnum.Cape, StateEnum.Cape, StateEnum.Small },
        new[] { StateEnum.Fire, StateEnum.Fire, StateEnum.Fire, StateEnum.Small }
    };

    private static readonly List<List<int>> _actionTable = new()
    {
        new List<int> { +100, +200, +300, 0 },
        new List<int> { 0, 200, 300, -100 },
        new List<int> { 0, 0, 0, -200 },
        new List<int> { 0, 0, 0, -300 }
    };

    public MarioStateMachine2()
    {
        Score = 0;
        State = StateEnum.Small;
    }

    public int Score { get; private set; }

    public StateEnum State { get; private set; }

    /// <summary>
    /// 得到蘑菇
    /// </summary>
    public void ObtainMushRoom()
    {
        ExecuteEvent(EventEnum.GotMushRoom);
    }


    /// <summary>
    /// 得到斗篷
    /// </summary>
    public void ObtainCape()
    {
        ExecuteEvent(EventEnum.GotCape);
    }

    /// <summary>
    /// 得到火焰
    /// </summary>
    public void ObtainFireFlower()
    {
        ExecuteEvent(EventEnum.GotFire);
    }

    /// <summary>
    /// 遇到怪物
    /// </summary>
    public void MeetMonster()
    {
        ExecuteEvent(EventEnum.MetMonState);
    }

    private void ExecuteEvent(EventEnum @event)
    {
        var stateValue = (int)State;
        var eventValue = (int)@event;
        State = _transitionTable[stateValue][eventValue];
        Score = _actionTable[stateValue][eventValue];
    }
}

public enum EventEnum
{
    /// <summary>
    /// 得到蘑菇
    /// </summary>
    GotMushRoom,

    /// <summary>
    /// 得到斗篷
    /// </summary>
    GotCape,

    /// <summary>
    /// 得到火焰
    /// </summary>
    GotFire,

    /// <summary>
    /// 遇到怪物
    /// </summary>
    MetMonState
}